package codec;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

/**
 * Created by ryan on 15-8-25.
 */
public class FileRespondEncoder extends ProtocolEncoderAdapter {

    public void encode(IoSession ioSession, Object message, ProtocolEncoderOutput output) throws Exception {
        FileRespond fr = (FileRespond) message;
        byte[] bytes = fr.getContent();
        int capacity = 8 + 8 + 4 + bytes.length;
        IoBuffer buffer = IoBuffer.allocate(capacity, false);
        buffer.putLong(fr.getOffset());
        buffer.putLong(fr.getRecord());
        buffer.putInt(fr.getLength());
        buffer.put(bytes);
        buffer.flip();
        output.write(buffer);
        //System.out.println("Sent OK!");
    }
}
